﻿// (c) Copyright Webber-Cross Software Ltd.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using WebberCross.Obelisk.MVVMDemo.Model;
using System.Collections.Generic;
using System;
using System.Linq;
using System.Collections.ObjectModel;

namespace WebberCross.Obelisk.MVVMDemo.ViewModel
{
    public class MainViewModel : TombstonePersistentBase
    {
        private string _notes;
        private readonly ObservableCollection<Person> _people = new ObservableCollection<Person>();
        private Person _person;

        public string ApplicationTitle
        {
            get { return "OBELISK MVVM LIGHT"; }
        }

        public string PageName
        {
            get { return "Demo page"; }
        }

        /// <summary>
        /// This attribute tells the TombstoneHelper to store this property on dehydration
        /// Because this is a complex type, it must be registered when attaching the TombstoneHelper
        /// An array is used to marshal the List object as it's easier to deserialise
        /// </summary>
        [Persist(PersistMode.Both)]
        public Person[] PeopleArray
        {
            get 
            { 
                if(this._people != null)
                    return this._people.ToArray();

                return null;
            }
            set 
            {
                if (value != null)
                {
                    foreach(Person p in value)
                        this._people.Add(p);
                }
            }
        }

        public ObservableCollection<Person> People
        {
            get { return this._people; }
        }

        /// <summary>
        /// This attribute tells the TombstoneHelper to store this property on dehydration
        /// Because this is a complex type, it must be registered when attaching the TombstoneHelper
        /// </summary>
        [Persist(PersistMode.Both)]
        public Person Person
        {
            get { return this._person; }
            set 
            {
                if (this._person != value)
                {
                    this._person = value;
                    base.RaisePropertyChanged("Person");
                }
            }
        }

        /// <summary>
        /// This attribute tells the TombstoneHelper to store this property on dehydration
        /// </summary>
        [Persist(PersistMode.TombstonedOnly)]
        public string Notes
        {
            get { return this._notes; }
            set
            {
                if (this._notes != value)
                {
                    this._notes = value;
                    base.RaisePropertyChanged("Notes");
                }
            }
        }

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {

        }

        #region Obelisk methods

        /// <summary>
        /// Occurs after a class has been instantiated for the first time
        /// </summary>
        public override void OnLaunched()
        {
            // Typically data might be loaded from a service, but this is an example
            this._people.Add(new Person() { Name = "Person A", Birthday = new DateTime(1, 1, 1) });
            this._people.Add(new Person() { Name = "Person B", Birthday = new DateTime(2, 2, 2) });
            this._people.Add(new Person() { Name = "Person C", Birthday = new DateTime(3, 3, 3) });
            this._people.Add(new Person() { Name = "Person D", Birthday = new DateTime(4, 4, 4) });
            this._people.Add(new Person() { Name = "Person E", Birthday = new DateTime(5, 5, 5) });
            this._people.Add(new Person() { Name = "Person R", Birthday = new DateTime(6, 6, 6) });
        }

        /// <summary>
        /// Occurs after an instance has been rehydrated after an activation
        /// </summary>
        public override void OnActivated(bool preserved)
        {

        }

        /// <summary>
        /// Occurs before an instance is dehydrated the tombstoned
        /// </summary>
        public override void OnDeactivated()
        {

        }

        /// <summary>
        /// Occurs when the application is closing
        /// </summary>
        public override void OnClosing()
        {

        }

        #endregion
    }
}